using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;

namespace PhaseZero.GlobalNavigation.FeatureReceivers
{
    public class SiteMapFeatureReceiver : SPFeatureReceiver
    {
        #region Feature Events

        /// <summary>
        ///     Not Implemented
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            // no implementation
        }

        /// <summary>
        ///     Not Implemented
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // no implementation
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite site = (SPSite)properties.Feature.Parent;
                SPWebApplication webApp = SPWebApplication.Lookup(new Uri(site.Url));

                if (webApp != null)
                    UpdateWebConfig(webApp, false);
                else
                    throw new ApplicationException("Could not locate a web application");

                //Write log entry for success
                System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                el.Source = "SiteMapFeature";
                el.WriteEntry("Activation complete.");
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                el.Source = "SiteMapFeature";
                el.WriteEntry(ex.Message);
            }

        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite site = (SPSite)properties.Feature.Parent;
                SPWebApplication webApp = SPWebApplication.Lookup(new Uri(site.Url));

                if (webApp != null)
                    UpdateWebConfig(webApp, true);
                else
                    throw new ApplicationException("Could not locate a web application");

                //Write log entry for success
                System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                el.Source = "SiteMapFeature";
                el.WriteEntry("De-Activation complete.");
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                el.Source = "SiteMapFeature";
                el.WriteEntry(ex.Message);
            }
        }
        #endregion
        #region Update Web.Config
        private static void UpdateWebConfig(SPWebApplication webApp, bool removeModification)
        {
            string value =
                "<add name=\"LawSiteMapProvider\" siteMapFile=\"_layouts/PhaseZero/law.sitemap\" type=\"System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" />";
            SPWebConfigModification modification = new SPWebConfigModification();
                
            modification.Path = "configuration/system.web/siteMap/providers";
            modification.Name = "PZSiteMap";
            modification.Owner = "PhaseZero";
            modification.Value = value;
            modification.Sequence = 0;
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            if (removeModification)
                webApp.WebConfigModifications.Remove(modification);
            else
                webApp.WebConfigModifications.Add(modification);

            SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }


        #endregion

    }
}
